package ar.edu.unicen.exa.isistan.archassis.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MiscUtils {
	
	//"http://svn.codespot.com/a/eclipselabs.org/logviewer/trunk/"
	
	public static String plainFileName(String weirdText) {
		if (weirdText == null) {
			return "null";
		}
		String result = weirdText.replaceAll("http://", "");
		result = result.replaceAll("https://", "");
		result = result.replaceAll("/", "_");
		result = result.replaceAll(":", "_");
		while (result.endsWith("_")) {
			result = result.substring(0, result.length() -1);
		}
		return result;
	}
	
	public static void main(String[] args) {
		System.out.println(plainFileName("http://svn.codespot.com/a/eclipselabs.org/logviewer/trunk/"));
	}

	public static String checkFile(String filePath) {
		File file = new File(filePath);
		if (!file.isFile()) {
			System.err.println("El archivo '" + filePath + "' no existe!");
		}
		return file.getAbsolutePath();
	}

	public static void createNullClass(File srcFolderV0) {
		File nullpackageFile = new File(srcFolderV0, "nullpackage");
		nullpackageFile.mkdirs();
		File nullClassFile = new File(nullpackageFile, "Null.java");
		StringBuffer nullClass = new StringBuffer("package nullpackage;");
		nullClass.append("public class Null {\n")
			.append("\tpublic Null() {\n")
			.append("\t}\n")
			.append("}\n");
		writeFile(nullClass.toString(), nullClassFile);
	}
	public static void writeFile(String text, File file) {
		try {
			FileWriter outFile = new FileWriter(file);
			PrintWriter out = new PrintWriter(outFile);
			out.println(text);
			out.close();
		} catch (IOException e){
			e.printStackTrace();
		}
	}
}
